package com.apes.crm.vip.masterdata.service;

import com.apes.crm.vip.masterdata.model.VipLabelLevel;
import com.apes.crm.vip.masterdata.repository.VipLabelLevelRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 功能：会员级别 Service
 *
 * @author HuJianlin
 * @create 2018-03-15 14:35
 */
@Service("vipLabelLevelService")
public class VipLabelLevelService extends DomainService {

    @Autowired
    VipLabelLevelRepository vipLabelLevelRepository;

    public VipLabelLevel save(SimpleRequest request) {
        VipLabelLevel vipLevel = request.getO(VipLabelLevel.class);
        return vipLabelLevelRepository.saveAndFlush(vipLevel);
    }

    public VipLabelLevel update(SimpleRequest request) {
        String id = request.get("id");
        int version = request.get("version");

        // 从数据库中取数，检查数据
        VipLabelLevel oldVipLevel = vipLabelLevelRepository.findOne(id);
        if (oldVipLevel == null) throw new RuntimeException("会员级别【" + id + "】在系统中不存在。");
        if (oldVipLevel.getVersion() != version) throw new RuntimeException("记录已更新或已删除，请重新提交！");

        VipLabelLevel vipLevel = request.getO(VipLabelLevel.class);
        return vipLabelLevelRepository.saveAndFlush(vipLevel);
    }

    public List<VipLabelLevel> findAll(SimpleRequest request) {
        return vipLabelLevelRepository.findAll();
    }

}
